<?php

/**
 * The Orno Component Library
 * 
 * @author Phil Bennett @philipobenito
 * @license MIT (see the LICENSE file)
 */
namespace System\Di\Definition;

use System\Di\ContainerInterface;

class Factory
{

    /**
     * 返回一个基于类型的具体定义
     * 
     * @param string $alias 别名
     * @param mixed $concrete
     * @param \Orno\Di\ContainerInterface $container 容器
     * @param boolean $callable 是否回调
     * @return mixed
     */
    public function __invoke($alias, $concrete, ContainerInterface $container, $callable = FALSE)
    {
        if ($callable === true) {
            return new CallableDefinition($alias, $concrete, $container);
        }
        
        if ($concrete instanceof \Closure) {
            return new ClosureDefinition($alias, $concrete, $container);
        }
        
        if (is_string($concrete) && class_exists($concrete)) {
            return new ClassDefinition($alias, $concrete, $container);
        }
        
        // if the item is not defineable we just return the value to be stored
        // in the container as an arbitrary value/instance
        return $concrete;
    }
}
